package com.open.capacity.activity.controller;

import com.open.capacity.activity.service.ActivityService;
import com.open.capacity.common.model.Activity;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: open-capacity-platform
 * @description:
 * @author: bellweather
 * @create: 2021-01-18 16:17
 **/

@RestController
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping("/activitys")
    public PageResult<Activity> getList(@RequestParam Map<String,Object> params){
        PageUtil.pageParamConver(params,true);
        List<Activity> list = activityService.getList(params);
        return PageResult .<Activity>builder().count((long) list.size()).data(list).code(0).build();
    }


    /*/** 
    * @Description: 返回未被删除的信息     注：逻辑删除，不是物理删除
    * @Param: [params] 筛选条件
    * @return: com.open.capacity.common.web.PageResult<com.open.capacity.common.model.Activity> 
    * @Author: bellweather
    * @Date: 2021/1/19 
    */ 
    @GetMapping("/activitys/enable")
    public PageResult<Activity> getListEnable(@RequestParam Map<String,Object> params){
        params.put("deleted",1);
        PageUtil.pageParamConver(params,true);
        List<Activity> list = activityService.getList(params);
        return PageResult .<Activity>builder().count((long) list.size()).data(list).code(0).build();
    }

    /*/**
    * @Description: 添加活动信息，添加成功则返回活动id
    * @Param: [activity]
    * @return: com.open.capacity.common.web.Result
    * @Author: bellweather
    * @Date: 2021/1/19
    */
    @PutMapping("/activity")
    public Result save(@RequestBody Activity activity){
        int save = activityService.save(activity);
        return save>0?Result.succeed(activity.getId(),"操作成功！"):Result.failed("请稍后重试！");
    }

    
    /*/** 
    * @Description: 修改信息  修改成功返回Id,修改失败返回 null
    * @Param: [activity] 
    * @return: com.open.capacity.common.web.Result 
    * @Author: bellweather
    * @Date: 2021/1/19 
    */ 
    @PostMapping("/activity")
    public Result update(@RequestBody Activity activity){
        int update = activityService.update(activity);
        return update>0?Result.succeed(activity.getId(),"修改成功"):Result.failed("修改失败");
    }

    /*/**
    * @Description: 逻辑删除信息 删除成功返回Id,删除失败返回 null
    * @Param: [id]
    * @return: com.open.capacity.common.web.Result
    * @Author: bellweather
    * @Date: 2021/1/19
    */
    @DeleteMapping("/activity/{id}/{deleted}")
    public Result delete(@PathVariable("id")int id,@PathVariable("deleted") int deleted){
        int delete = activityService.delete(id,deleted);
        return delete>0?Result.succeed(id,"修改成功"):Result.failed("修改失败");
    }

    @DeleteMapping("/activity/{id}")
    public Result delete(@PathVariable("id")int id){
        int delete = activityService.realdelete(id);
        return delete>0?Result.succeed(id,"删除成功"):Result.failed("删除失败");
    }




}
